package Thread;

/**
 * @author: lqh
 * @description: 多线程学习
 * @program: Demo
 * @create: 2018-04-04 14:36
 **/
public class ThreadDemo implements Runnable {

    private Thread thread;
    private String threadName;

    public ThreadDemo(String threadName) {
        this.threadName = threadName;
        System.out.println("Creating " + threadName);
    }

    @Override
    public void run() {
        System.out.println("Running: " + threadName);
        for (int i = 0; i < 4; i++) {
            try {
                System.out.println("Thread: " + threadName + ", " + i);
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Thread " + threadName + " exiting.");
        }
    }

    public void start() {
        System.out.println("Starting " +  threadName );
        if (thread == null) {
            thread = new Thread(this, threadName);
            thread.setName("xxxx"); //改变线程名称S
            thread.setPriority(Thread.MAX_PRIORITY); //改变线程优先级
            thread.start();
        }
    }

    public static void main(String[] args) {
        ThreadDemo threadDemo = new ThreadDemo("thread-001");
        threadDemo.start();

        ThreadDemo threadDemo2 = new ThreadDemo("thread-002");
        threadDemo2.start();

    }
}
